//
//  BeautyDetailViewController.m
//  Car
//
//  Created by 何晓文 on 15/9/28.
//  Copyright © 2015年 朱鸿儒. All rights reserved.
//

#import "BeautyDetailViewController.h"
#import "UMSocial.h"
#import "MineTableViewController.h"
#import "EnterInformationHandle.h"
@interface BeautyDetailViewController ()<UIWebViewDelegate,UMSocialUIDelegate>
@property (nonatomic, strong)UIWebView *webView;
@property (nonatomic, strong) UIButton *close;
@property (nonatomic, strong) UIButton *shareButton;
@property (nonatomic, strong) UIButton *collectButton;

@property (nonatomic, strong) UMSocialData *socialData;
@property (nonatomic, strong) UMSocialBar *socialBar;
@end

@implementation BeautyDetailViewController


/**
 *  加载论坛详情
 */
- (void)loadWebData {
	self.webView.hidden = NO;
	NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.webUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5];
	[self.webView loadRequest:request];
	[ProgressHUD show:@"玩命加载中" Interaction:YES];
	
}


//移除详情页面动画
- (void)removeView {

	[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark <-<-<-<-------initButton------->->->->

- (void)initCloseButton {
	
	//关闭按钮
	self.close = [UIButton buttonWithType:(UIButtonTypeCustom)];
	[self.close setImage:[UIImage imageNamed:@"552cc3334ab03_64"] forState:(UIControlStateNormal)];
	self.close.frame = CGRectMake(kWidth- 60 ,kHeight - 35, 30, 30);
	[self.close addTarget:self action:@selector(removeView) forControlEvents:(UIControlEventTouchUpInside)];
	
	[self.webView addSubview:self.close];
	
}
/**
 *  分享按钮
 */
- (void)initshareButton {

	//分享按钮
	self.shareButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
	self.shareButton.frame = CGRectMake(30, kHeight - 35, 30, 30);
	[self.shareButton setImage:[UIImage imageNamed:@"552cc62440096_64"] forState:(UIControlStateNormal)];
	[self.shareButton addTarget:self action:@selector(shareSomething) forControlEvents:(UIControlEventTouchUpInside)];
	[self.webView addSubview:self.shareButton];

}
/**
 *收藏按钮
 */
- (void)initCollectButton {
	
	self.collectButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
	self.collectButton.frame = CGRectMake((kWidth- 30) / 2 ,kHeight - 35, 30, 30);
	[self.collectButton setImage:[UIImage imageNamed:@"552cc1903424d_64"] forState:(UIControlStateNormal)];
	[self.collectButton addTarget:self action:@selector(collectCurrentWeb) forControlEvents:(UIControlEventTouchUpInside)];
	[self.webView addSubview:self.collectButton];
	
}
//收藏方法
- (void)collectCurrentWeb {
    if ([EnterInformationHandle share].enterOrNot == YES) {
	[[MyFavorite shareFavorite] addBeautyModel:self.model WithUser:[EnterInformationHandle share].myNumber];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"登录后,才可以收藏!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }

}
#pragma mark <-<-<-<-------分享------->->->->
- (void)shareSomething {

	[UMSocialSnsService presentSnsIconSheetView:self
										 appKey:@"5606df08e0f55a58d1003b4b"
									  shareText:[NSString stringWithFormat:@"%@ %@", self.model.title, self.webUrl]
									 shareImage:[UIImage imageNamed:@"icon"]
								shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToQQ, UMShareToWechatTimeline, UMShareToTwitter,UMShareToWechatSession,UMShareToDouban, nil]
									   delegate:self];


}
#pragma mark <-<-<-<-------lifeCircle------->->->->

- (void)viewDidLoad {
    [super viewDidLoad];
	[self loadWebData];
	
	
[UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina].snsClickHandler(self,[UMSocialControllerService defaultControllerService],YES);
}
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:YES];
	[ProgressHUD dismiss];
	
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
	NSLog(@"开始加载");
	
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
	[ProgressHUD showError:@"网络繁忙"];
	[self performSelector:@selector(removeView) withObject:nil afterDelay:1];
//	[self removeView];
	NSLog(@"%@",error);
}
#pragma mark <-<-<-<-------处理json网页内容------->->->->
//webView加载完成的时候调用
- (void)webViewDidFinishLoad:(UIWebView *)webView {
	
	//删除回复点赞
	NSMutableString *js2 = [NSMutableString string];
	[js2 appendString:@"var handle = document.getElementsByClassName('handle')[0];"];
	[js2 appendString:@"handle.parentNode.removeChild(handle);"];
	[webView stringByEvaluatingJavaScriptFromString:js2];
	NSMutableString *js1 = [NSMutableString string];
	//删除回复列表
	[js1 appendString:@"var post = document.getElementsByClassName('post-flow')[0];"];
	[js1 appendString:@"post.parentNode.removeChild(post);"];
	[webView stringByEvaluatingJavaScriptFromString:js1];
	
	//删除论坛信息
	NSMutableString *js3 = [NSMutableString string];
	[js3 appendString:@"var post = document.getElementsByClassName('forum-info')[0];"];
	[js3 appendString:@"post.parentNode.removeChild(post);"];
	[webView stringByEvaluatingJavaScriptFromString:js3];
	
	/**
		添加按钮
		详情显示到window上
	 */
	NSLog(@"加载完成");
	[ProgressHUD dismiss];

	[UIView animateWithDuration:0.5 animations:^{
		self.webView.alpha = 1;
		self.webView.frame = kMainScreen;
		[self.view addSubview:self.webView];

	}completion:^(BOOL finished) {
		[self initToolBar];
		[self initCloseButton];
		[self initshareButton];
		[self initCollectButton];
	}];
}
//添加低栏
- (void)initToolBar {
	
	UIImageView *toolImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, kHeight - 40 , kWidth, 40)];
	toolImage.backgroundColor = [UIColor whiteColor];
	[self.webView addSubview:toolImage];
	UIView *toolView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 1)];
	toolView.backgroundColor = [UIColor grayColor];
	[toolImage addSubview:toolView];

}
- (UIWebView *)webView {
	if (!_webView) {
		_webView = [[UIWebView alloc]initWithFrame:CGRectMake(kWidth / 2, kHeight / 2, 1, 1)];
		_webView.delegate = self;
		_webView.alpha = 0.2;
	}
	return _webView;
}

//-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
//{
//	UIView * iconActionSheet = [self.tabBarController.view viewWithTag:kTagSocialIconActionSheet];
//	
//	[iconActionSheet setNeedsDisplay];
//	UIView * shakeView = [self.tabBarController.view viewWithTag:kTagSocialShakeView];
//	[shakeView setNeedsDisplay];
//}

@end
